{include file="header.html"}
<body class="gr">

<div class="navBar">
	<a class="left" href="javascript:history.back()"><i class="return"></i></a>
	<div class="titleView-x">收货地址</div>
	<a class="right" href="{if $is_wx==1}javascript:getWxAddress(){else}wap.php?tpl=address.add_address{/if}"><span>添加</span></a>
</div>

<div class="address-list main-padding-top">
	<div class="pullRefresh">
		<section class="list">
			{foreach from=$data name=g item=g}
			<ul class="tableView tableView-noLine">
				<li>
					<h1>
						<div class="name">{$g->contactman}　{$g->mobile}</div>
						<div class="address">{$g->province}{if $g->province!=$g->city} {$g->city}{/if} {$g->district} {$g->address}</div>
					</h1>
					<div class="ge-bottom ge-light"></div>
					<div class="bottomView">
						<form method="post" action="api.php?app=address&act=delete_address">
						<input type="hidden" name="gourl" value="wap.php?app=address&act=list_address" />
						<input type="hidden" name="id" value="{$g->id}" />
						<a href="javascript:void(0)" class="btn delete" skip="skip"><i></i><span>删除</span></a>
						</form>
						<a href="wap.php?app=address&act=get_address&id={$g->id}" class="btn edit"><i></i><span>编辑</span></a>
						<form method="post" action="api.php?app=address&act=set_default">
						<input type="hidden" name="gourl" value="wap.php?app=address&act=list_address" />
						<input type="hidden" name="id" value="{$g->id}" />
						<a href="javascript:void(0)" class="default {if $g->is_default==1}default-x{/if}">设为默认</a>
						</form>
					</div>
				</li>
			</ul>
			{/foreach}
		</section>
	</div>
</div>

{include file="footer.html"}
<script>
var offset = $('.pullRefresh .list > ul').length;
function createHtml(g){
	var html = '<ul class="list tableView tableView-noLine">\
		<li>\
			<h1>\
				<div class="name">'+g.contactman+'　'+g.mobile+'</div>\
				<div class="address">'+(g.province+(g.province!=g.city?' '+g.city:'')+' '+g.district+' '+g.address)+'</div>\
			</h1>\
			<div class="ge-bottom ge-light"></div>\
			<div class="bottomView">\
				<form method="post" action="api.php?app=address&act=delete_address">\
				<input type="hidden" name="gourl" value="wap.php?app=address&act=list_address" />\
				<input type="hidden" name="id" value="'+g.id+'" />\
				<a href="javascript:void(0)" class="btn delete" skip="skip"><i></i><span>删除</span></a>\
				</form>\
				<a href="wap.php?app=address&act=edit_address&id='+g.id+'" class="btn edit"><i></i><span>编辑</span></a>\
				<form method="post" action="api.php?app=address&act=set_default">\
				<input type="hidden" name="gourl" value="wap.php?app=address&act=list_address" />\
				<input type="hidden" name="id" value="'+g.id+'" />\
				<a href="javascript:void(0)" class="default '+(g.is_default==1?'default-x':'')+'">设为默认</a>\
				</form>\
			</div>\
		</li>\
	</ul>';
	offset++;
	return html;
}
function setTouchEnd(){
	$('.delete:not([touchend])').ontouchend(function(){
		if(!confirm('真的要删除吗？'))return false;
		$(this).parent().submit();
	}).each(function(){
		$(this).attr('touchend', 'touchend');
	});
	$('.default:not([touchend])').ontouchend(function(){
		$(this).parent().submit();
	}).each(function(){
		$(this).attr('touchend', 'touchend');
	});
}
$(function(){
	setTouchEnd();
	$('.address-list').height($.window().height-$('.address-list').padding().top);
	$('.pullRefresh').pullRefresh({
		header : true,
		footer : true,
		footerNoMoreText : '- END -',
		refresh : function(fn){
			var _this = this;
			offset = 0;
			$.getJSON('api.php?app=address&act=list_address', function(json){
				if(json.error!=0){ $.overloadError(json.msg);return; }
				var html = '';
				if($.isArray(json.data))for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
				_this.find('.list').html(html);
				setTouchEnd();
				fn();
			});
		},
		load : function(fn){
			var _this = this;
			$.getJSON('api.php?app=address&act=list_address', { offset:offset }, function(json){
				if(json.error!=0){ $.overloadError(json.msg);return; }
				var html = '';
				if($.isArray(json.data))for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
				_this.find('.list').append(html);
				setTouchEnd();
				fn();
			});
		}
	});
});

//获取微信地址
{if isset($weixin_sign_package)}
wx.config({
	debug: false,
	appId: "{$weixin_sign_package.appId}",
	timestamp: "{$weixin_sign_package.timestamp}",
	nonceStr: "{$weixin_sign_package.nonceStr}",
	signature: "{$weixin_sign_package.signature}",
	jsApiList: [
		//所有要调用的 API 都要加到这个列表中
		'openAddress'
	]
});
wx.ready(function(){
	/*//
	wx.checkJsApi({
		jsApiList: [ //需要检测的JS接口列表
			'openAddress'
		],
		success: function(res){
			alert($.debug(res));
		}
    });
	//*/
});
function getWxAddress(){
	wx.openAddress({
		success: function(res){
			if(res.errMsg == 'openAddress:ok'){
				var userName = res.userName; //收货人姓名
				var telNumber = res.telNumber; //收货人手机号码
				var nationalCode = res.nationalCode; //地址行政区码
				var postalCode = res.postalCode; //邮编
				var provinceName = res.provinceName; //省
				var cityName = res.cityName; //市
				var countryName = res.countryName; //区
				var detailInfo = res.detailInfo; //详细地址信息
				if(!userName.length){ $.overloadError('地址信息缺少收货人姓名');return }
				if(!telNumber.length){ $.overloadError('地址信息缺少收货人手机号码');return }
				$.postJSON('api.php?app=address&act=add_address', {
					contactman : userName,
					mobile : telNumber,
					province : provinceName,
					city : cityName,
					district : countryName,
					address : detailInfo
				}, function(json){
					if(json.error!=0){ $.overloadError(json.msg);return }
					$('.pullRefresh').beginPullRefresh();
				});
			}else{
				if(!confirm(res.errMsg+'\n是否手动填写？'))return;
				location.href = 'wap.php?tpl=address.add_address';
			}
		}
	});
}
{/if}
</script>